package com.android.lovely.ui.chat

import android.widget.SeekBar
import androidx.core.graphics.toColorInt
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.android.lovely.R
import com.android.lovely.databinding.FragmentChatListBinding
import com.android.lovely.databinding.FragmentChatStyleBinding
import com.android.lovely.framework.BaseFragment
import com.android.lovely.framework.BaseMvvmFragment
import com.android.lovely.ui.chat.ChatStyleWidget.OnStyleClickListener
import com.android.lovely.ui.viewmodel.ChatViewModel
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.view.ViewExt.clickable
import kotlinx.coroutines.flow.collect

/**
 *
 * @author why
 * @date 2025/3/31 01:25
 */
class ChatStyleFragment: BaseFragment<FragmentChatStyleBinding>() {
    private val mViewModel by activityViewModels<ChatStyleViewModel>()


    private var isUser = false

    override fun initView() {
        isUser = (arguments?.getInt("id", 0) ?: 0) == 1
        update()
        mBinding.dialogueColor.setOnStyleClickListener(object : OnStyleClickListener{
            override fun onStyleClick(index: Int, color: String) {
                mViewModel.updateRoleStyle {
                    if(isUser) {
                        mViewModel.updateUserStyle { dialogueColor = color.toColorInt() }
                    } else {
                        mViewModel.updateRoleStyle { dialogueColor = color.toColorInt() }
                    }
                }
            }
        })
        mBinding.describingColor.setOnStyleClickListener(object : OnStyleClickListener{
            override fun onStyleClick(index: Int, color: String) {
                mViewModel.updateRoleStyle {
                    if(isUser) {
                        mViewModel.updateUserStyle { descColor = color.toColorInt() }
                    } else {
                        mViewModel.updateRoleStyle { descColor = color.toColorInt() }
                    }
                }
            }
        })
        mBinding.otherColor.setOnStyleClickListener(object : OnStyleClickListener{
            override fun onStyleClick(index: Int, color: String) {
                mViewModel.updateRoleStyle {
                    if(isUser) {
                        mViewModel.updateUserStyle { otherColor = color.toColorInt() }
                    } else {
                        mViewModel.updateRoleStyle { otherColor = color.toColorInt() }
                    }
                }
            }
        })
        mBinding.bg.setOnStyleClickListener(object : OnStyleClickListener{
            override fun onStyleClick(index: Int, color: String) {
                if(isUser) {
                    mViewModel.updateUserStyle {
                        backgroundColor = color.toColorInt()
                        backgroundColorAlpha = mBinding.seekbar.progress
                    }
                } else {
                    mViewModel.updateRoleStyle {
                        backgroundColor = color.toColorInt()
                        backgroundColorAlpha = mBinding.seekbar.progress
                    }
                }
            }
        })
        mBinding.tvItalic.clickable {
            mBinding.tvItalic.isSelected = !mBinding.tvItalic.isSelected
            if (isUser) {
                mViewModel.updateUserStyle { dialogueItalic = mBinding.tvItalic.isSelected }
            } else {
                mViewModel.updateRoleStyle { dialogueItalic = mBinding.tvItalic.isSelected }
            }
        }
        mBinding.tvDescItalic.clickable {
            val isSelect = !mBinding.tvDescItalic.isSelected
            mBinding.tvDescItalic.isSelected = isSelect
            if (isUser) {
                mViewModel.updateUserStyle { descItalic = isSelect }
            } else {
                mViewModel.updateRoleStyle { descItalic = isSelect }
            }
        }
        mBinding.tvOtherItalic.clickable {
            val isSelect = !mBinding.tvOtherItalic.isSelected
            mBinding.tvOtherItalic.isSelected = isSelect
            if (isUser) {
                mViewModel.updateUserStyle { otherItalic = isSelect }
            } else {
                mViewModel.updateRoleStyle { otherItalic = isSelect }
            }
        }
        mBinding.seekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                mBinding.tvPercent.text = "$progress%"

                updateTextPosition()
                if(isUser) {
                    mViewModel.updateUserStyle {
                        backgroundColor = mBinding.bg.getColor().toColorInt()
                        backgroundColorAlpha = progress

                    }
                } else {
                    mViewModel.updateRoleStyle {
                        backgroundColor = mBinding.bg.getColor().toColorInt()
                        backgroundColorAlpha = progress
                    }
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // 开始拖动时的处理
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                // 停止拖动时的处理
            }
        })

        lifecycleScope.launchSafety{
            mViewModel.updateFlow.collect{
                update()
            }
        }
    }

    fun update() = if (isUser) {
        mBinding.dialogueColor.setColor(mViewModel.defaultStyle.user.dialogueColor)
        mBinding.describingColor.setColor(mViewModel.defaultStyle.user.descColor)
        mBinding.otherColor.setColor(mViewModel.defaultStyle.user.otherColor)
        mBinding.bg.setColor(mViewModel.defaultStyle.user.backgroundColor)


        mBinding.seekbar.progress = mViewModel.defaultStyle.user.backgroundColorAlpha
        mBinding.tvPercent.text = "${mViewModel.defaultStyle.user.backgroundColorAlpha}%"
        mBinding.tvItalic.isSelected =  mViewModel.defaultStyle.user.dialogueItalic
        mBinding.tvDescItalic.isSelected = mViewModel.defaultStyle.user.descItalic
        mBinding.tvOtherItalic.isSelected = mViewModel.defaultStyle.user.otherItalic

        updateTextPosition()
    } else {

        mBinding.dialogueColor.setColor(mViewModel.defaultStyle.role.dialogueColor)
        mBinding.describingColor.setColor(mViewModel.defaultStyle.role.descColor)
        mBinding.otherColor.setColor(mViewModel.defaultStyle.role.otherColor)
        mBinding.bg.setColor(mViewModel.defaultStyle.role.backgroundColor)
        mBinding.tvItalic.isSelected =  mViewModel.defaultStyle.role.dialogueItalic
        mBinding.tvDescItalic.isSelected =  mViewModel.defaultStyle.role.descItalic
        mBinding.tvOtherItalic.isSelected =  mViewModel.defaultStyle.role.otherItalic

        mBinding.seekbar.progress = mViewModel.defaultStyle.role.backgroundColorAlpha
        mBinding.tvPercent.text = "${mViewModel.defaultStyle.role.backgroundColorAlpha}%"

        updateTextPosition()

    }

    private fun updateTextPosition() {
        val thumbX = mBinding.seekbar.thumb.bounds.centerX()
        val parentWidth = mBinding.seekbar.width
        val tvWidth = mBinding.tvPercent.width

        var newX = thumbX - tvWidth / 2
        if (newX < 0) {
            newX = 0
        } else if (newX + tvWidth > parentWidth) {
            newX = parentWidth - tvWidth
        }
        mBinding.tvPercent.x = newX.toFloat()
    }

    override suspend fun initData() {

    }
}

